package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

import Model.*;
import Controller.*;

public class MainFrame extends JFrame {
	
	static final int HEIGHT= 450;
	static final int WIDTH=750;
	
	static String groupesStr="Groupes";
	static String dateStr="Date";
	static String alphaStr="A-Z";
	static String triStr="Tri : ";
	static String groupeStr="Groupe : ";
	static String mailStr="Mail : ";
	static String phoneStr="Téléphone";
	static String siteStr="Site Web : ";
	static String adresseStr="Adresse : ";
	static String dateAjoutStr="Date d'ajout : ";
	static String modifierStr="Modifier";
	static String supprimerStr="Supprimer";
	static String addStr="Ajouter Contact";
	
	
	Container bigPanel;
	JPanel contactPanel,mainPanel, menuPanel, viewPanel,formPanel;
	GridBagConstraints c ;
	
	
	//Sortpanel
	JLabel sortTitle;
	JButton sortAlpha, sortDate, sortGroup;
	
	//contactsPanel
	JList list;
	DefaultListModel model;
	int counter = 15;
	JScrollPane pane;
	JButton addButton;
	JTable table;
	
	
	
	
	//ViewPanel
	JLabel pictureLbl,completNameLbl,nameLbl, firstNameLbl,groupLbl, mailLbl, phoneLbl, websiteLbl, adressLbl, dateLbl;
	JLabel groupTitle, mailTitle, phoneTitle, websiteTitle,adressTitle,dateTitle;
	JButton modifyButton, deleteButton;
	ImageIcon profilePicture;
	
	
	//FormPanel
	JTextField nameTF, firstNameTF, adressTF, phoneTF, websiteTF, mailTF, groupTF;
	JButton saveButton;
	int modifyMode;
	private DefaultTableModel tableModel;
	private JButton chooseBtn;
	
	public MainFrame(ArrayList<Person> contactList)
	{
		super("AdresseBook");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setSize(new Dimension(WIDTH, HEIGHT));
		bigPanel=this.getContentPane();
		
		
		this.setBigPanel();
		this.setMainPanel();
		this.setMenuPanel();
		this.setContactPanel(contactList);
		this.setViewPanel();
		this.setFormPanel();
	    
		
		this.setVisible(true);
		
	}
	
	private void setBigPanel()
	{
		menuPanel=new JPanel();
		mainPanel=new JPanel();
		contactPanel=new JPanel();
		viewPanel=new JPanel();
		formPanel=new JPanel();
		
		bigPanel.setBackground(Color.gray);
		menuPanel.setBackground(new Color(225,223,223));
		viewPanel.setBackground(new Color(249,249,249));
		formPanel.setBackground(new Color(249,249,249));
		
		
		
		bigPanel.setLayout(new GridBagLayout());
		c = new GridBagConstraints();
	
		
		
		
		c.fill = GridBagConstraints.HORIZONTAL;
	    c.gridwidth=1;
	    c.gridheight=1;
	    c.weightx=1;
	    //c.weighty=1;
		c.gridx=0;
		c.gridy = 0;
		
		//bigPanel.add(menuPanel,c);
		
		c.fill = GridBagConstraints.BOTH;
		c.weighty=1;
	    c.gridwidth=1;
		c.gridx=0;
		c.gridy=1;
		
		bigPanel.add(mainPanel,c);
		
		
	}
	
	
	private void setMenuPanel()
	{
		menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		sortTitle = new JLabel(triStr);
		sortAlpha = new JButton(alphaStr);
		sortDate = new JButton(dateStr);
		sortGroup = new JButton(groupesStr);
		
		Border paddingBorder = BorderFactory.createEmptyBorder(0,20,0,20);
		sortTitle.setBorder(paddingBorder); 
		sortAlpha.setBorder(paddingBorder); 
		sortDate.setBorder(paddingBorder); 
		sortGroup.setBorder(paddingBorder); 
	
		
		menuPanel.add(sortTitle);
		menuPanel.add(sortAlpha);
		menuPanel.add(sortDate);
		menuPanel.add(sortGroup);
		
	}
	
	private void setMainPanel()
	{
		mainPanel.setLayout(new GridLayout(1,2));
		
		mainPanel.add(contactPanel);
		mainPanel.add(viewPanel);
	}
	
	private void setContactPanel(ArrayList<Person> contactList)
	{
		

		    
		    model = new DefaultListModel();
		    list = new JList(model);
		    // pane = new JScrollPane(list);
		    //this.loadContactList(contactList);
		    
		    String[] entetes = {"Prénom", "Nom", "Groupe", "Date d'ajout"};
		    Object[][] donnees={{}};
		    tableModel = new DefaultTableModel(donnees, entetes);
		    
		    this.loadContactList(contactList);
		    
		   
		   
		    
		    table=new JTable(tableModel);
		    table.setAutoCreateRowSorter(true);
		    pane=new JScrollPane(table);
		    addButton = new JButton(addStr);
		    
	
		   
		    contactPanel.setLayout(new BorderLayout());
		    contactPanel.add(pane, BorderLayout.CENTER);
		    contactPanel.add(addButton, BorderLayout.SOUTH);
		    
		    
		    
		    
		
	

}
	
	
	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}

	/**
	 * @return the list
	 */
	public JList getList() {
		return list;
	}

	private void setViewPanel()
	{
		viewPanel.setLayout(new GridLayout(9,2));
		
		groupTitle=new JLabel(groupeStr);
		mailTitle=new JLabel(mailStr);
		phoneTitle=new JLabel(phoneStr);
		websiteTitle=new JLabel(siteStr);
		adressTitle=new JLabel(adresseStr);
		dateTitle=new JLabel(dateStr);
		
		profilePicture=new ImageIcon("Resources/test.png");
		pictureLbl=new JLabel();
		pictureLbl.setIcon(profilePicture);
		
		completNameLbl=new JLabel();
		nameLbl=new JLabel();
		firstNameLbl=new JLabel();
		mailLbl=new JLabel();
		groupLbl=new JLabel();
		phoneLbl=new JLabel();
		websiteLbl=new JLabel();
		adressLbl=new JLabel();
		dateLbl=new JLabel();
		
		modifyButton=new JButton(modifierStr);
		deleteButton=new JButton(supprimerStr);
		
		viewPanel.add(pictureLbl);
		viewPanel.add(new JLabel());
		viewPanel.add(nameLbl);
		viewPanel.add(firstNameLbl);
		viewPanel.add(groupTitle);
		viewPanel.add(groupLbl);
		viewPanel.add(mailTitle);
		viewPanel.add(mailLbl);
		viewPanel.add(phoneTitle);
		viewPanel.add(phoneLbl);
		viewPanel.add(websiteTitle);
		viewPanel.add(websiteLbl);
		viewPanel.add(adressTitle);
		viewPanel.add(adressLbl);
		viewPanel.add(dateTitle);
		viewPanel.add(dateLbl);
		viewPanel.add(modifyButton);
		viewPanel.add(deleteButton);
		
		UIManager.put("Label.font", UIManager.getFont("Label.font").deriveFont((float)20.0));
		SwingUtilities.updateComponentTreeUI(viewPanel);
		nameLbl.setFont(nameLbl.getFont().deriveFont(Font.BOLD));
		this.selectFirstContact();
		
		
		
		
	}
	
	private void setFormPanel()
	{
		formPanel.setLayout(new GridLayout(9,1));
		
		
		chooseBtn=new JButton("Importer une photo");
		nameTF=new JTextField("Nom :", 20);
		firstNameTF=new JTextField("Prénom :", 20);
		groupTF=new JTextField(groupeStr, 20);
		mailTF=new JTextField(mailStr, 20);
		phoneTF=new JTextField(phoneStr, 20);
		websiteTF=new JTextField(siteStr, 20);
		adressTF=new JTextField(adresseStr, 20);
        saveButton=new JButton("Sauvegarder");
        
        formPanel.add(nameTF);
        formPanel.add(firstNameTF);
        formPanel.add(groupTF);
        formPanel.add(mailTF);
        formPanel.add(phoneTF);
        formPanel.add(websiteTF);
        formPanel.add(adressTF);
        formPanel.add(chooseBtn);
        formPanel.add(saveButton); 
		
	}
	
	
	private void loadContactList(ArrayList<Person> contactList)
	{
		model.clear();
		for(Person p : contactList)
		{
			model.addElement(p.toString());
		}
		tableModel.setRowCount(0);
		SimpleDateFormat formatDateJour = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS"); 
		for(Person p : contactList)
		{
			
			String[] rowData={p.getFirstName(), p.getName(),p.getGroup(),formatDateJour.format(p.getDate())};
			tableModel.addRow(rowData);
		}
		
		
		
	}
	
	public DefaultListModel getListModel()
	{
		return model;
	}
	
	public void setProfile(Person p)
	{
		nameLbl.setText(p.getName());
		firstNameLbl.setText(p.getFirstName());
		groupLbl.setText(p.getGroup());
		mailLbl.setText(p.getEmail());
		phoneLbl.setText(p.getPhoneNumber());
		websiteLbl.setText(p.getHomePage());
		adressLbl.setText(p.getCompanyAdress());
		dateLbl.setText(p.getDateToString());
		Image scaledImg=new ImageIcon(p.getImageURL()).getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT);
		pictureLbl.setIcon( new ImageIcon(scaledImg));
		
		
		
	}

	/**
	 * @return the sortAlpha
	 */
	public JButton getSortAlpha() {
		return sortAlpha;
	}

	/**
	 * @return the sortDate
	 */
	public JButton getSortDate() {
		return sortDate;
	}

	/**
	 * @return the sortGroup
	 */
	public JButton getSortGroup() {
		return sortGroup;
	
	}
	/**
	 * @return the addButton
	 */
	public JButton getAddButton() {
		return addButton;
	
	}
	/**
	 * @return the modifyButton
	 */
	public JButton getModifyButton() {
		return modifyButton;
	}
	/**
	 * @return the saveButton
	 */
	public JButton getSaveButton() {
		return saveButton;
	}
	/**
	 * @return the websiteLbl
	 */
	public JLabel getWebsiteLbl() {
		return websiteLbl;
	}
	/**
	 * @return the mailLbl
	 */
	public JLabel getMailLbl() {
		return mailLbl;
	}
	/**
	 * @return the deleteButton
	 */
	public JButton getDeleteButton() {
		return deleteButton;
	}
	
	public void reloadList(ArrayList<Person> newList)
	{
		this.loadContactList(newList);
		list.setModel(model);
		
	}
	
	public void switchToAddView()
	{
		mainPanel.remove(viewPanel);
		mainPanel.add(formPanel);
		this.setContentPane(bigPanel);
		
		nameTF.setText("Nom :");
		firstNameTF.setText("Prénom :");
		groupTF.setText("Groupe :");
		mailTF.setText("Adresse Email :");
		phoneTF.setText("Numéro de téléphone :");
		websiteTF.setText("Site Web :");
	    adressTF.setText("Adresse :");
	}
	
	public void switchToProfileView()
	{
		mainPanel.remove(formPanel);
		mainPanel.add(viewPanel);
		this.setContentPane(bigPanel);
	}
	
	public void switchToModifyView()
	{
		mainPanel.remove(viewPanel);
		mainPanel.add(formPanel);
		this.setContentPane(bigPanel);
		
		nameTF.setText(this.nameLbl.getText());
		firstNameTF.setText(this.firstNameLbl.getText());
		groupTF.setText(this.groupLbl.getText());
		mailTF.setText(this.mailLbl.getText());
		phoneTF.setText(this.phoneLbl.getText());
		websiteTF.setText(this.websiteLbl.getText());
	    adressTF.setText(adressLbl.getText());
		
	}
	
	public ArrayList<String> getContactData()
	{
		ArrayList<String> dataset=new ArrayList<String>();
		dataset.add(nameTF.getText());
		dataset.add(firstNameTF.getText());
		dataset.add(groupTF.getText());
		dataset.add(mailTF.getText());
		dataset.add(phoneTF.getText());
		dataset.add(websiteTF.getText());
		dataset.add(adressTF.getText());
		dataset.add(dateLbl.getText());
		
		
		
		return dataset;
	}

	/**
	 * @return the modifyMode
	 */
	public int getModifyMode() {
		return modifyMode;
	}

	/**
	 * @param modifyMode the modifyMode to set
	 */
	public void setModifyMode(int modifyMode) {
		this.modifyMode = modifyMode;
	}
	
	public void selectFirstContact()
	{
		list.setSelectedIndex(0);
		
	}

	/**
	 * @return the nameLbl
	 */
	public JLabel getNameLbl() {
		return nameLbl;
	}

	/**
	 * @return the firstNameLbl
	 */
	public JLabel getFirstNameLbl() {
		return firstNameLbl;
	}
	public void popup(String s)
	{
		JOptionPane.showMessageDialog(this,s);
	}

	/**
	 * @return the dateLbl
	 */
	public JLabel getDateLbl() {
		return dateLbl;
	}

	/**
	 * @return the chooseBtn
	 */
	public JButton getChooseBtn() {
		return chooseBtn;
	}
	
}
